/*txBx9FIYfUQl2UukqRyP7yZAQjenf+1adeilhAiKzCRgS1A5Mx9hjQJlpB0IBJF8*/
/*
 * @Author: xiahui 
 * @PageInfo: 保存 
 * @Date: 2019-03-06 16:30:47 
 * @Last Modified by: xiahui
 * @Last Modified time: 2019-07-10 15:28:30
 */
import { ajax } from 'nc-lightapp-front';
import { URL, PAGECODE, UISTATUS, AREA } from '../../constance';
import { showSuccessInfo } from '../../../../../scmpub/scmpub/pub/tool/messageUtil.js';
import { buttonControl } from '../viewControl/buttonControl';
import { getLangByResId } from '../../../../../scmpub/scmpub/pub/tool/multiLangUtil';

export default function saveButton(props) {
	let allRows = props.editTable.getAllRows(AREA.listTableId);

	let grid = {
		pageid: PAGECODE.listPagecode,
		model: {
			areacode: AREA.listTableId,
			areaType: 'table',
			rows: []
		}
	};

	let rowIndex = [];
	allRows.forEach((row, index) => {
		if (row.status != '0') {
			grid.model.rows.push(row);
			rowIndex.push(index);
		}
	});

	props.validateToSave(grid, () => {
		ajax({
			url: URL.save,
			data: grid,
			success: (res) => {
				if (res.success) {
					if (res.data) {
						let updateData = [];
						res.data[AREA.listTableId].rows.map((row, i) => {
							updateData.push({ index: rowIndex[i], data: row });
						});
						props.editTable.updateDataByIndexs(AREA.listTableId, updateData, true, true);
					}
					showSuccessInfo(getLangByResId(this, '4004PAYPLAN-000003')); /* 国际化处理： 保存成功!*/
					// 卡片界面，在浏览态时勾选行，点修改后去掉勾选
					this.props.editTable.selectAllRows(AREA.listTableId, false);
					this.setState({ status: UISTATUS.browse });
					buttonControl.call(this, props, UISTATUS.browse);
				}
			}
		});
	});
}

/*txBx9FIYfUQl2UukqRyP7yZAQjenf+1adeilhAiKzCRgS1A5Mx9hjQJlpB0IBJF8*/